package com.xk5156.widget;

import android.content.Intent;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import androidx.constraintlayout.widget.Constraints;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
  private RecyclerView rvList;
  private LabelAdapter mLabelAdapter;
  private List<String> dataList = new ArrayList<>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();
  }

  private void initViews() {
    dataList.add(Constants.WIDGET_WAVE_VIEW);
    rvList = findViewById(R.id.rv_list);
    rvList.setLayoutManager(new LinearLayoutManager(this));
    mLabelAdapter = new LabelAdapter(dataList);
    rvList.setAdapter(mLabelAdapter);
    mLabelAdapter.setOnItemChildClickListener((adapter, view, position) -> {
      switch (view.getId()){
        case R.id.btn_label:
          String label= (String) adapter.getData().get(position);
          Bundle bundle=new Bundle();
          bundle.putString(Constants.KEY_WIDGET_LABEL,label);
          Intent intent = new Intent(MainActivity.this, WidgetActivity.class);
          intent.putExtras(bundle);
          startActivity(intent);
          break;
        default:break;
      }
    });
  }

  public static class LabelAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public LabelAdapter(@Nullable List<String> data) {
      super(R.layout.layout_item_label,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
      helper.addOnClickListener(R.id.btn_label);
      helper.setText(R.id.btn_label,item);
    }
  }
}